/**
 * 
 */

package cn.cn77.futures;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

import cn.cn77.futures.utils.C2Sever;
import cn.cn77.futures.utils.MyApplication;
import cn.cn77.futures.utils.UserSharedPreference;
import cn.cn77.futures.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sun 登录页面
 */
public class LoginActivity extends BaseActivity implements OnClickListener {
	private EditText phoneNoEt, passwordEt;
	private CheckBox cb;
	private Button loginBt;
	private String phone;
	private String password;
	protected boolean cbState = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);
		MyApplication.getInstance().addActivity(this);
		initView();
	}

	private void initView() {
		phoneNoEt = (EditText) findViewById(R.id.edt_phone_number);
		passwordEt = (EditText) findViewById(R.id.edt_password);
		cb = (CheckBox) findViewById(R.id.ctv_save);
		cb.setChecked(cbState);
		cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				cbState = isChecked;
			}
		});

		if (cbState) {
			CharSequence lastUserName = UserSharedPreference.getParams(
					UserSharedPreference.MOBILE, this) == null ? ""
					: UserSharedPreference.getParams(
							UserSharedPreference.MOBILE, this);
			CharSequence lastPassword = UserSharedPreference.getParams(
					UserSharedPreference.Password, this) == null ? ""
					: UserSharedPreference.getParams(
							UserSharedPreference.Password, this);
			phoneNoEt.setText(lastUserName);
			passwordEt.setText(lastPassword);
		}
		loginBt = (Button) findViewById(R.id.btn_login);
		loginBt.setOnClickListener(this);
	}

	/**
	 * 判断数据是否合法，合法则返回包含数据的map
	 * 
	 * @return
	 */
	private Map<String, String> inputVerify() {
		String phone = phoneNoEt.getText().toString();
		String password = passwordEt.getText().toString();
		// 1.验证号码
		if (phone == null || "".equals(phone)) {
			phoneNoEt.setHint("号码不能为空");
			passwordEt.setHintTextColor(getResources().getColor(R.color.red));
			return null;
		}
		// 2.验证密码
		if (password == null && "".equals(password)) {
			Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
			return null;
		}

		// 输入手机号和密码合法之后放入map返回
		Map<String, String> dataMap = new HashMap<String, String>();
		dataMap.put("phone", phone);
		dataMap.put("password", password);
		return dataMap;
	}

	/**
	 * 登录按钮的监听事件
	 * 
	 * @param view
	 */
	public void login(View view) {
		loginBt.setText("正在登录……");
		Map<String, String> data = inputVerify();
		if (data == null) {
			loginBt.setText("登 录");
			return;
		}
		phone = data.get("phone");
		password = data.get("password");
		setUrl(C2Sever.Controller.User, C2Sever.Action.Login);
		// 请求参数
		mParams.put(C2Sever.Field.Login.Phone, phone);
		mParams.put(C2Sever.Field.Login.Password,
				Utils.encodePassword(password));
		// 3.执行登录操作登录
		doAsyncPost(this);
	}

	@Override
	protected void failureTest(JSONObject o, int status) {
		loginBt.setText("登 录");
	}

	@Override
	public void successWithData(JSONObject data, int status) {
		try {
			saveDataToLocal(data);
			toActivity(this, MainActivity.class);
			finish();
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			loginBt.setText("登 录");
		}
	}

	/**
	 * 登录成功之后，将此次的登录时间戳、用户的手机号码、以及数据库返回回来的用户id保存到本地sharePreference
	 * 
	 * @param content
	 * @throws JSONException
	 */
	private void saveDataToLocal(JSONObject content) throws JSONException {
		JSONObject o = content.getJSONObject(C2Sever.Field.Other);
		UserSharedPreference.saveParams(this, UserSharedPreference.TimeStamp,
				o.getString(C2Sever.Field.Login.TimeStamp));
		UserSharedPreference.saveParams(this, UserSharedPreference.MOBILE,
				phone);
		UserSharedPreference.saveParams(this, UserSharedPreference.Password,
				password);
		UserSharedPreference.saveParams(this, UserSharedPreference.USER_ID,
				o.getString(C2Sever.Field.Uid));
	}

	@Override
	public void failure(String content) {
		super.failure(content);
		loginBt.setText("登录");
	}

	@Override
	public void onClick(View v) {
		login(v);
	}
}
